package com.sunshine.lnuplus.model.room

import android.content.Context
import android.os.AsyncTask
import androidx.lifecycle.LiveData
import com.sunshine.lnuplus.model.room.TimetableDatabase.Companion.getDatabase
import com.sunshine.lnuplus.utils.DateUtils

class TimetableRepository(context: Context) {
    val allLiveUser: LiveData<List<User?>?>?
    val allLiveLesson: LiveData<List<Lesson?>?>?
    val allLiveCountDown: LiveData<List<CountDown?>?>?
    private val timetableDao: TimetableDao?
    fun insertUser(vararg users: User?) {
        InsertUser(timetableDao).execute(*users)
    }

    fun insertLesson(vararg lessons: Lesson?) {
        InsertLesson(timetableDao).execute(*lessons)
    }

    fun insertCountDown(vararg countDowns: CountDown?) {
        InsertCountDown(timetableDao).execute(*countDowns)
    }

    fun updateUser(vararg users: User?) {
        UpdateUser(timetableDao).execute(*users)
    }

    fun updateLesson(lesson: Lesson, oldLessonName: String, oldDayOfWeek: Int, oldStart: Int, oldEnd: Int) {
        timetableDao?.updateLesson(
            oldLessonName,
            oldDayOfWeek,
            oldStart,
            oldEnd,
            lesson.lessonName,
            lesson.dayOfWeek,
            lesson.start,
            lesson.end,
            lesson.teacher,
            lesson.location,
            lesson.weeks,
            lesson.color
        )
    }

    fun updateBackgroundColor(lessonName: String, backgroundColor: String){
        timetableDao?.updateBackgroundColor(lessonName, backgroundColor)
    }

    fun updateCountDown(vararg countDowns: CountDown?) {
        UpdateCountDown(timetableDao).execute(*countDowns)
    }

    fun updateCountDownName(oldLessonName: String, lessonName: String){
        timetableDao?.updateCountDownName(oldLessonName, lessonName)
    }

    fun updateCountDownLocation(lessonName: String, location: String){
        timetableDao?.updateCountDownLocation(lessonName, location)
    }

    fun updateCountDownTime(lessonName: String, time: String, year: Int, month: Int, day: Int, hour: Int, minute: Int){
        timetableDao?.updateCountDownTime(lessonName, time, year, month, day, hour, minute)
    }

    fun deleteLesson(vararg lessons: Lesson?) {
        DeleteLesson(timetableDao).execute(*lessons)
    }

    fun deleteCountDown(lessonName: String){
        timetableDao!!.deleteCountDown(lessonName)
    }

    fun getLesson(): List<Lesson> {
        val lessonList = ArrayList<Lesson>()
        timetableDao!!.allLesson.forEach {
            if(newLesson(lessonList, it)){
                lessonList.add(it)
            }
        }
        return lessonList
    }

    //判断是否有除了课程号完全相同的课程
    private fun newLesson(
        lessonList: List<Lesson>,
        lesson: Lesson
    ): Boolean {
        lessonList.forEach {
            if(it.lessonName == lesson.lessonName && it.dayOfWeek == lesson.dayOfWeek && it.start == lesson.start && it.end == lesson.end && it.location == lesson.location && it.teacher == lesson.teacher && it.weeks == lesson.weeks){
                return false
            }
        }
        return true
    }

    fun getCountDown(): List<CountDown> {
        return timetableDao!!.allCountDown
    }

    fun getTodayCountDown() : List<CountDown>{
        return timetableDao!!.getTodayCountDown(DateUtils.year, DateUtils.month - 1, DateUtils.day)
    }

    fun deleteAllLesson() {
        timetableDao!!.deleteAllLesson()
    }

    internal class InsertUser(private val timetableDao: TimetableDao?) : AsyncTask<User?, Void?, Void?>() {
        override fun doInBackground(vararg users: User?): Void? {
            timetableDao!!.insertUser(*users)
            return null
        }
    }

    internal class InsertLesson(private val timetableDao: TimetableDao?) : AsyncTask<Lesson?, Void?, Void?>() {
        override fun doInBackground(vararg lessons: Lesson?): Void? {
            timetableDao!!.insertLesson(*lessons)
            return null
        }
    }

    internal class InsertCountDown(private val timetableDao: TimetableDao?) : AsyncTask<CountDown?, Void?, Void?>() {
        override fun doInBackground(vararg countDowns: CountDown?): Void? {
            timetableDao!!.insertCountDown(*countDowns)
            return null
        }
    }

    internal class UpdateUser(private val timetableDao: TimetableDao?) : AsyncTask<User?, Void?, Void?>() {
        override fun doInBackground(vararg users: User?): Void? {
            timetableDao!!.updateUser(*users)
            return null
        }
    }

    internal class UpdateCountDown(private val timetableDao: TimetableDao?) : AsyncTask<CountDown?, Void?, Void?>() {
        override fun doInBackground(vararg countDowns: CountDown?): Void? {
            timetableDao!!.updateCountDown(*countDowns)
            return null
        }
    }

    internal class DeleteLesson(private val timetableDao: TimetableDao?) : AsyncTask<Lesson?, Void?, Void?>() {
        override fun doInBackground(vararg lessons: Lesson?): Void? {
            timetableDao!!.deleteLesson(*lessons)
            return null
        }
    }

    init {
        val database = getDatabase(context)
        timetableDao = database.timetableDao
        allLiveUser = timetableDao!!.allLiveUser
        allLiveLesson = timetableDao.allLiveLesson
        allLiveCountDown = timetableDao.allLiveCountDown
    }
}